<?php

namespace app\erp\model;

use think\Model;

class Yujingdb extends Model {
	protected $pk = 'yujing_id';
	protected $name = 'yujing';
	protected $type = [ 
			'create_time' => 'timestamp:Y-m-d H:i',
			'push_supplier_time' => 'timestamp:Y-m-d H:i',
			'supplier_confirm_time' => 'timestamp:Y-m-d H:i',
			'gys_fh_time' => 'timestamp:Y-m-d H:i',
			'gys_sh_time' => 'timestamp:Y-m-d H:i',
			'caigou_invoice_time' => 'timestamp:Y-m-d H:i',
			'caigou_hetong_time' => 'timestamp:Y-m-d H:i',
			'jc_fh_time' => 'timestamp:Y-m-d H:i',
			'jc_sh_time' => 'timestamp:Y-m-d H:i',
			'invoice_time' => 'timestamp:Y-m-d H:i',
			'audit_caigou_invoice_time' => 'timestamp:Y-m-d H:i',
			'pay_time' => 'timestamp:Y-m-d H:i',
			'ghs_pay_time' => 'timestamp:Y-m-d H:i' 
	];
	// 自动完成时间转换
	protected $autoWriteTimestamp = true;
	public $param = array ();
	public function base($query) {
	    // 排除450和451的订单
        $query->where('yujing_id','-1');
        /*$not_sql = Orderdb::where('supplier_id','IN',array(450,451,432))->column('order_no');
        $query->where('order_no','NOT IN',$not_sql);
		if (isset ( $this->param ['order_no'] ) && $this->param ['order_no']) {
			$query->where ( 'order_no', $this->param ['order_no'] );
		}
		if (isset ( $this->param ['type_index'] ) && $this->param ['type_index']) {
			if ($this->param ['type_index'] == - 1) {
				$query->typeIndexNo ();
			} else {
				$query->typeIndex ( $this->param ['type_index'] );
			}
		}*/
	}
	public function scopeTypeIndexNo($query) {
		$siteConfigdb = new SiteConfigdb ();
		$siteConfigdb->param ['config_name'] = 'YUJING';
		$get_config = $siteConfigdb->getValue ();
		$get_config_arr = explode ( "\n", $get_config );
		// 指派
		$diff_time0 = explode ( ":", $get_config_arr [0] );
		$time = (time () - $diff_time0 [1] * 3600);
		$sql = '(create_time is not null and create_time < ' . $time . ' and push_supplier_time is null)';
		// 确认
		$diff_time1 = explode ( ":", $get_config_arr [1] );
		$time = (time () - $diff_time1 [1] * 3600);
		$sql .= ' or (push_supplier_time is not null and push_supplier_time < ' . $time . ' and push_supplier_time is null)';
		// 发货
		$diff_time2 = explode ( ":", $get_config_arr [2] );
		$time = (time () - $diff_time2 [1] * 3600);
		$sql .= ' or (supplier_confirm_time is not null and supplier_confirm_time < ' . $time . ' and gys_fh_time is null)';
		// 收货
		$diff_time3 = explode ( ":", $get_config_arr [3] );
		$time = (time () - $diff_time3 [1] * 3600);
		$sql .= ' or (gys_fh_time is not null and gys_fh_time < ' . $time . ' and gys_sh_time is null)';
		// 上传发票
		$diff_time4 = explode ( ":", $get_config_arr [4] );
		$time = (time () - $diff_time4 [1] * 3600);
		$sql .= ' or (gys_sh_time is not null and gys_sh_time < ' . $time . ' and caigou_invoice_time is null)';
		// 审核发票
		$diff_time5 = explode ( ":", $get_config_arr [5] );
		$time = (time () - $diff_time5 [1] * 3600);
		$sql .= ' or (caigou_invoice_time is not null and caigou_invoice_time < ' . $time . ' and audit_caigou_invoice_time is null)';
		// 上传采购合同
		$diff_time6 = explode ( ":", $get_config_arr [6] );
		$time = (time () - $diff_time6 [1] * 3600);
		$sql .= ' or (gys_sh_time is not null and gys_sh_time < ' . $time . ' and caigou_hetong_time is null)';
		// 同步军采发货
		$diff_time7 = explode ( ":", $get_config_arr [7] );
		$time = (time () - $diff_time7 [1] * 3600);
		$sql .= ' or (gys_fh_time is not null and gys_fh_time < ' . $time . ' and jc_fh_time is null)';
		// 同步军采收货
		$diff_time8 = explode ( ":", $get_config_arr [8] );
		$time = (time () - $diff_time8 [1] * 3600);
		$sql .= ' or (gys_sh_time is not null and gys_sh_time < ' . $time . ' and jc_sh_time is null)';
		// 同步军采上传发票
		$diff_time9 = explode ( ":", $get_config_arr [9] );
		$time = (time () - $diff_time9 [1] * 3600);
		$sql .= ' or (jc_fh_time is not null and jc_fh_time < ' . $time . ' and invoice_time is null)';
		// 同步收货收款
		$diff_time10 = explode ( ":", $get_config_arr [10] );
		$time = (time () - $diff_time10 [1] * 3600);
		$sql .= ' or (jc_sh_time is not null and jc_sh_time < ' . $time . ' and pay_time is null)';
		// 同步收货付款
		$diff_time11 = explode ( ":", $get_config_arr [11] );
		$time = (time () - $diff_time11 [1] * 3600);
		$sql .= ' or (pay_time is not null and pay_time < ' . $time . ' and ghs_pay_time is null)';
		$query->where ( '', 'exp', $sql );
	}
	public function scopeTypeIndex($query, $type_index) {
		$siteConfigdb = new SiteConfigdb ();
		$siteConfigdb->param ['config_name'] = 'YUJING';
		$get_config = $siteConfigdb->getValue ();
		$get_config_arr = explode ( "\n", $get_config );
		$true_index = $type_index;
		$get_diff_time_arr = explode ( ":", $get_config_arr [$type_index - 2] );
		$get_diff_time = - $get_diff_time_arr [1];
		switch ($type_index) {
			case 2 :
				// N时间没有指派预警
				$query->whereTime ( 'create_time', 'not null' );
				$query->whereTime ( 'create_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'push_supplier_time', 'null' );
				break;
			case 3 :
				// N时间没有确认预警
				$query->whereTime ( 'push_supplier_time', 'not null' );
				$query->whereTime ( 'push_supplier_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'supplier_confirm_time', 'null' );
				break;
			case 4 :
				// 确认订单后N时间没有发货预警
				$query->whereTime ( 'supplier_confirm_time', 'not null' );
				$query->whereTime ( 'supplier_confirm_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'gys_fh_time', 'null' );
				break;
			case 5 :
				// 发货后N时间没有收货预警
				$query->whereTime ( 'gys_fh_time', 'not null' );
				$query->whereTime ( 'gys_fh_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'gys_sh_time', 'null' );
				break;
			case 6 :
				// 收货后N时间没有上传采购发票预警
				$query->whereTime ( 'gys_sh_time', 'not null' );
				$query->whereTime ( 'gys_sh_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'caigou_invoice_time', 'null' );
				break;
			case 7 :
				// 库房上传采购发票后N时间没审核预警
				$query->whereTime ( 'caigou_invoice_time', 'not null' );
				$query->whereTime ( 'caigou_invoice_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'audit_caigou_invoice_time', 'null' );
				break;
			case 8 :
				// 收货后N时间没上传采购合同预警
				$query->whereTime ( 'gys_sh_time', 'not null' );
				$query->whereTime ( 'gys_sh_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'caigou_hetong_time', 'null' );
				break;
			case 9 :
				// 库房发货后N时间没有同步军网发货预警
				$query->whereTime ( 'gys_fh_time', 'not null' );
				$query->whereTime ( 'gys_fh_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'jc_fh_time', 'null' );
				break;
			case 10 :
				// 库房收货后N时间没有同步军网收货预警
				
				$query->whereTime ( 'gys_sh_time', 'not null' );
				$query->whereTime ( 'gys_sh_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'jc_sh_time', 'null' );
				break;
			case 11 :
				// 军采同步发货后N时间没有上传用户发票预警
				$query->whereTime ( 'jc_fh_time', 'not null' );
				$query->whereTime ( 'jc_fh_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'invoice_time', 'null' );
				break;
			case 12 :
				// 军采同步收货后N时间没收款预警
				$query->whereTime ( 'jc_sh_time', 'not null' );
				$query->whereTime ( 'jc_sh_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'pay_time', 'null' );
				break;
			case 13 :
				// 收款后N时间没付款预警
				$query->whereTime ( 'pay_time', 'not null' );
				$query->whereTime ( 'pay_time', '<', $get_diff_time . ' hours' );
				$query->where ( 'ghs_pay_time', 'null' );
				break;
		}
	}
	public function ajaxSelect() {
		$return ['data'] = $this->ajaxList ();
		$return ['count'] = $this->ajaxCount ();
		return $return;
	}
	public function ajaxList() {
		$order_by = 'create_time';
		$siteConfigdb = new SiteConfigdb ();
		$siteConfigdb->param ['config_name'] = 'YUJING';
		$get_config = $siteConfigdb->getValue ();
		$get_config_arr = explode ( "\n", $get_config );
		if (isset ( $this->param ['type_index'] ) && $this->param ['type_index']) {
			switch ($this->param ['type_index']) {
				case 2 :
					// N时间没有指派预警
					$order_by = 'create_time';
					break;
				case 3 :
					// N时间没有确认预警
					$order_by = 'push_supplier_time';
					break;
				case 4 :
					// 确认订单后N时间没有发货预警
					$order_by = 'supplier_confirm_time';
					break;
				case 5 :
					// 发货后N时间没有收货预警
					$order_by = 'gys_fh_time';
					break;
				case 6 :
					// 收货后N时间没有上传采购发票预警
					$order_by = 'gys_sh_time';
					break;
				case 7 :
					// 库房上传采购发票后N时间没审核预警
					$order_by = 'caigou_invoice_time';
					break;
				case 8 :
					// 收货后N时间没上传采购合同预警
					$order_by = 'gys_sh_time';
					break;
				case 9 :
					// 库房发货后N时间没有同步军网发货预警
					$order_by = 'gys_fh_time';
					break;
				case 10 :
					// 库房收货后N时间没有同步军网收货预警
					$diff_time = explode ( ":", $get_config_arr [8] );
					$order_by = 'jc_sh_time';
					break;
				case 11 :
					// 军采同步发货后N时间没有上传用户发票预警
					$order_by = 'jc_fh_time';
					break;
				case 12 :
					// 军采同步收货后N时间没收款预警
					$order_by = 'jc_sh_time';
					break;
				case 13 :
					// 收款后N时间没付款预警
					$order_by = 'pay_time';
					break;
			}
		}
		/*
		if($this->param['type_index'] == 11){
			$data = $this->fetchSql(true)->page ( $this->param ['page'], $this->param ['limit'] )->order ( $order_by . ' asc' )->select ();
			print_r($data);
			exit();
		}
		*/
		$data = $this->page ( $this->param ['page'], $this->param ['limit'] )->order ( $order_by . ' asc' )->select ();
		if (! $data->isEmpty ()) {
			$data = $data->toArray ();
			foreach ( $data as $key => $val ) {
				$data [$key] = $this->handleData ( $val, $get_config_arr );
				$temp = $data [$key] ['chaoshi'];
				$d = 0;
				$h = 0;
				$m = 0;
				if ($temp > 24 * 60) {
					$d = intval ( $temp / (24 * 60) );
				}
				if (($temp - $d * 24 * 60) > 60) {
					$h = intval ( ($temp - $d * 24 * 60) / 60 );
				}
				$m = ($temp - $d * 24 * 60 - $h * 60);
				$data [$key] ['chaoshi_text'] = ($d ? $d . '天' : '') . $h . '时' . $m . '分';
			}
		}
		return $data;
	}
	public function ajaxCount() {
		$count = $this->count ();
		return $count;
	}
	protected function handleData($val, $get_config_arr) {
        $val['yewuyuan'] = '';
        $val['gongyingshang'] = '';
	    $get_order = Orderdb::where('order_no',$val['order_no'])->find();
	    if($get_order){
	        $get_order = $get_order->toArray();
	        $get_member = Memberdb::where('member_id',$get_order['supplier_id'])->find();
	        if($get_member){
	            $get_member = $get_member->toArray();
	            $val['yewuyuan'] = $get_member['yewuyuan'];
	            $val['gongyingshang'] = $get_member['supplier_name'];
            }
        }
		if (! $val ['push_supplier_time']) {
			$temp = explode ( ':', $get_config_arr [0] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['create_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '指派预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
		if (! $val ['supplier_confirm_time'] && $val['push_supplier_time']) {
			$temp = explode ( ':', $get_config_arr [1] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['push_supplier_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '确认预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
		if (! $val ['gys_fh_time'] && $val['supplier_confirm_time']) {
			$temp = explode ( ':', $get_config_arr [2] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['supplier_confirm_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '发货预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
		if (! $val ['gys_sh_time'] && $val['gys_fh_time']) {
			$temp = explode ( ':', $get_config_arr [3] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['gys_fh_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '收货预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
		if (! $val ['caigou_invoice_time'] && $val['gys_sh_time']) {
			$temp = explode ( ':', $get_config_arr [4] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['gys_sh_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '采购发票预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
		if (! $val ['audit_caigou_invoice_time'] && $val['caigou_invoice_time']) {
			$temp = explode ( ':', $get_config_arr [5] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['caigou_invoice_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '审核发票预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
		if (! $val ['caigou_hetong_time'] && $val['gys_sh_time']) {
			$temp = explode ( ':', $get_config_arr [6] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['gys_sh_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '采购合同预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
		if (! $val ['jc_fh_time'] && $val['gys_fh_time']) {
			$temp = explode ( ':', $get_config_arr [7] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['gys_fh_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '军网发货预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
		if (! $val ['jc_sh_time'] && $val['gys_sh_time']) {
			$temp = explode ( ':', $get_config_arr [8] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['gys_sh_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '军网收货预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
		if (! $val ['invoice_time'] && $val['jc_fh_time']) {
			$temp = explode ( ':', $get_config_arr [9] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['jc_fh_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '上传发票预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
		if (! $val ['pay_time'] && $val['jc_sh_time']) {
			$temp = explode ( ':', $get_config_arr [10] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['jc_sh_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '军网收款预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
		if (! $val ['ghs_pay_time'] && $val['pay_time']) {
			$temp = explode ( ':', $get_config_arr [11] );
			$time = time () - $temp [1] * 3600 - strtotime ( $val ['pay_time'] );
			if ($time > 0) {
				$val ['yujing_text'] = '军网付款预警';
				$val ['chaoshi'] = intval ( $time / 60 );
				return $val;
			}
		}
	}
}